'''
Created on Oct 23, 2011

@author: baiamc
'''

import random
from blackjack.card import Card

class Deck(object):
    '''
    Class that simulates a deck of cards
    '''


    def __init__(self, decks):
        '''
        Constructor
        
        If decks is passed in, will shuffle together that many 52 card decks into one deck
        '''
        
        self._draw_deck = [] # Heap of cards in the draw pile
        self._discard_deck = [] # normal list of cards in discard pile
        if int(decks) >= 1:
            self.buildDeck(decks)
    
    def buildDeck(self, decks):
        '''
        Shuffle together one or more standard decks of cards
        '''
        
        if int(decks) < 1:
            decks = 1
        
        for deck in range(decks):
            for number in range(13):
                for suit in range(4):
                    self._draw_deck.append( (random.random(), Card(number, suit) ) )
        self._draw_deck.sort()
    
    def shuffle(self):
        '''
        Shuffle an existing deck
        '''
    
        temp = []
    
        #Pull cards from the draw and discard piles into a temp heap
        for card in self._draw_deck:
            temp.append( (random.random(), card) )
        
        for card in self._discard_deck:
            temp.append( (random.random(), card) )
        
        # place temp deck into the draw pile, and sort
        self._draw_deck=temp
        self._draw_deck.sort()
        
    def draw(self):
        '''
        Get the card on the top of the draw deck.
        
        Will reshuffle the discard deck if the draw deck is empty to get a new card.
        '''
        if len(self._draw_deck) == 0:
            self.shuffle()
        return self._draw_deck.pop()
        
    
    def discard(self,card):
        """
        Place a card onto the discard pile.
        """
        self._discard_desk.push(card)
       
                    
        
        
